using Microsoft.EntityFrameworkCore;
using StudentManagement.Domain.Entities;
using StudentManagement.Domain.Repositories;
using StudentManagement.Infrastructure.Data;

namespace StudentManagement.Infrastructure.Repositories;

public class StudentRepository : Repository<Student>, IStudentRepository
{
    public StudentRepository(ApplicationDbContext context) : base(context)
    {
    }

    public async Task<IEnumerable<Student>> GetStudentsByClassIdAsync(int classId)
    {
        return await _dbSet.Where(s => s.ClassId == classId).ToListAsync();
    }

    public async Task<Student> GetStudentByStudentIdAsync(string studentId)
    {
        return await _dbSet.FirstOrDefaultAsync(s => s.StudentId == studentId);
    }

    public async Task<IEnumerable<Student>> SearchStudentsAsync(string keyword)
    {
        if (string.IsNullOrWhiteSpace(keyword))
        {
            return await GetAllAsync();
        }

        keyword = keyword.ToLower();
        return await _dbSet
            .Where(s => s.StudentId.ToLower().Contains(keyword) ||
                        s.Name.ToLower().Contains(keyword) ||
                        s.Email.ToLower().Contains(keyword) ||
                        s.PhoneNumber.Contains(keyword))
            .ToListAsync();
    }

    public async Task<IEnumerable<Student>> GetStudentsWithScoresAsync()
    {
        return await _dbSet.Include(s => s.Scores).ToListAsync();
    }
}